/*
 * @Descripttion: 患者创建买药订单
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-05-04 11:29:33
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-04 17:18:37
 */
const DB = require('../../lib/DB')
const { Fail, Success } = require('../../lib/Response')

module.exports = async function (ctx, nect) {
    try {
        const id = (await DB.models.buy_medicine.create({
            patient_id: ctx.session.patient.id,
            medicine_lib_id: ctx.request.body.medicine_lib_id,
            amount: ctx.request.body.amount,
        })).toJSON().id
        const buy_medicine = await DB.models.buy_medicine.findOne({
            where: { id },
            include: [
                {
                    model: DB.models.patient,
                    as: 'patient'
                },
                {
                    model: DB.models.user,
                    as: 'nurse'
                },
                {
                    model: DB.models.medicine_lib,
                    as: 'medicine_lib'
                }
            ]
        })
        ctx.body = new Success(buy_medicine.toJSON())
    } catch (e) {
        ctx.body = new Fail(e.message)
    }
}
